﻿using System;
using System.Windows.Forms;
using Relocation.Data;
using Relocation.Com.Tools;
using Relocation.Com;
using Relocation.Base;
using Relocation.Base.UI;

namespace Relocation.UI
{
	public partial class PwdEdit : BaseForm
	{
		public PwdEdit()
			: base()
		{
			InitializeComponent();
		}

		public PwdEdit(Session session)
			: base(session)
		{
			InitializeComponent();
		}

		private void submit_Click(object sender, EventArgs e)
		{

			try
			{
				User user = this.Session.DataModel.getUserByNameAndPassword(Session.User.name, Relocation.Com.Encode.MD5.HashString(this.password.Text));
				if (user == null)
				{
					MyMessagebox.Show("旧密码输入错误！");
					return;
				}
				if (!Validator.Required(this.newPassword.Text))
				{
					MyMessagebox.Show("密码不能为空！");
					return;
				} else if (!Validator.length(this.newPassword.Text, 6, 12))
				{
					MyMessagebox.Show("密码长度为6-12位！");
					return;
				}
				if (!this.confirmPassword.Text.Equals(this.newPassword.Text))
				{
					MyMessagebox.Show("两次密码输入不一致！");
					return;
				}
				user.password = Relocation.Com.Encode.MD5.HashString(this.newPassword.Text);
				this.Session.DataModel.Save(user);
				MyMessagebox.Show("密码修改成功！");
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{
				MyMessagebox.Error("保存失败！");
				Log.Error(ex.GetInnerExceptionMessage());
				System.Diagnostics.Debug.WriteLine(ex);				
			}
			
		}
	}
}
